<?php

class Application_Model_DbTable_Vehicle extends Zend_Db_Table_Abstract
{

    protected $_name = 'vehiculo';
    protected $_sequence = false;

    public function getVehicle($placa)
    {
        $placa = (int)$placa;
        $row = $this->fetchRow('placa = ' . $placa);
        if (!$row) {
            throw new Exception("Could not find row $placa");
        }
        return $row->toArray();
    }
    
    public function addVehicle($placa, $anno,$marca,$modelo,$motor,$observaciones,$asignado)
    {
        $data = array(
            'placa' => $placa,
            'anno' => $anno,
             'marca' => $marca,
            'modelo' => $modelo,
            'motor' => $motor,
            'observaciones' => $observaciones,
            'asignado' => $asignado,
        );
        $this->insert($data);
    }
    
    public function updateVehicle($placa,$anno,$marca,$modelo,$motor,$observaciones,$asignado)
    {
        $data = array(
             'placa' => $placa,
            'anno' => $anno,
            'marca' => $marca,
            'modelo' => $modelo,
            'motor' => $motor,
            'observaciones' => $observaciones,
            'asignado' => $asignado,
        );
        $this->update($data, 'placa = ' . $placa);
    }
    
    public function deleteVehicle($placa)
    {
        try{    
             $this->delete('placa = ' . $placa);
        }catch(Zend_Exception $e){
            throw $e;
        }
    }

    public function findNotAssigned(){
        $row = $this->fetchall($this->select()
            ->where('asignado = ?', 'N'));
        if (!$row) {
            throw new Exception("Could not find row");
        }
        return $row->toArray();
    }

    public function updateAssigned($placa, $asignado){
       $data = array(
            'asignado' => $asignado,
        );
        $this->update($data, 'placa = ' . $placa);
    }

    public function find(){
        $row = $this->fetchall($this->select()
            ->limit(10, 0)
            ->order('fecha_insercion desc'));
        if (!$row) {
            throw new Exception("Could not find row");
        }
        return $row->toArray();
    }

    public function findUserVehicle($placas){
        $row = $this->fetchall($this->select()
            ->where('placa IN (?)',$placas));
        if (!$row) {
            throw new Exception("Could not find row");
        }
        return $row->toArray();
    }


    public function findVehicle($placa, $marca)
    {
        if(!empty($placa) && !empty($marca)){
            $placa = (int)$placa;

            $row = $this->fetchall($this->select()
              ->where('placa = ?', $placa)
              ->where('marca = ?', $marca)
            );
        } else {
            if(!empty($placa)){
                $placa = (int)$placa;

                $row = $this->fetchall($this->select()
                  ->where('placa = ?', $placa)
                );
            } else {
                $row = $this->fetchall($this->select()
                  ->where('marca = ?', $marca)
                );
            }
        }
        
        if (!$row) {
            throw new Exception("Could not find row $placa");
        }
        return $row->toArray();
    }



}

